﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Text;
using DataLayer.Controller;
using DataLayer.Utilities;
using System.Xml;
using System.IO;

/// <summary>
/// Summary description for AxesatPortal
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class AxesatPortal : System.Web.Services.WebService {
    
    public LoginInfo LoginCredential;
    public AxesatPortal () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    #region TestConnect
    [WebMethod(), SoapHeader("LoginCredential", Direction = SoapHeaderDirection.InOut)]
    public string HelloWorld() {
        try
        {
            if (authenticateSOAP())
                return "::::: HELLO WORLD :::::: - Login: " + LoginCredential.login + " - Password: axesat123";

            return "- Login or Password is incorrect.";
        }
        catch
        {
            return "- ERROR: Please authenticate first.";  
        }
    }
    #endregion

    #region Authentication SF
    private bool authenticateSOAP()
    {
        try
        {
            if (LoginCredential.login != cGlobal.WSUser || LoginCredential.password != cGlobal.GetMd5Hash(cGlobal.WSPwd))
            {
                LogErrorController.Insert(PropertiesApp.Error.Authenticate, "Login or Password is incorrect.");
                return false;
            }


            LogErrorController.Insert(PropertiesApp.Error.Authenticate, "Success");
            return true;
        }
        catch (Exception EX)
        {
            LogErrorController.Insert(PropertiesApp.Error.Authenticate, EX.ToString());
            return false;
        }
    }
    #endregion

    #region Methods SF

    //Creación OT
    [WebMethod(), SoapHeader("LoginCredential", Direction = SoapHeaderDirection.InOut)]
    public string OT_Creacion(string tipoRegistroSF, string codigoOrden, string idOT, string proveedor, string estado, string version, DateTime fecha, string CPA, string servicio, string idServicio, bool facturable, string razonSocial, string tipoCliente, string telefono, string direccion, string ciudad, string sede, string contacto, string celular, string observaciones, string caso, string trabajos, string tecnologia )
    {
        try
        {
            if (authenticateSOAP())
            {
                LogErrorController.Insert(PropertiesApp.Error.OTController, "Success");
                return OTController.Insert(tipoRegistroSF, codigoOrden, idOT, proveedor, estado, version, fecha, CPA, servicio, idServicio, facturable, razonSocial, tipoCliente, telefono, direccion, ciudad, sede, contacto, celular, observaciones, caso, trabajos, tecnologia); ;
            }

            return "- Login or Password is incorrect.";
        }
        catch (Exception EX)
        {
            LogErrorController.Insert(PropertiesApp.Error.OTController, EX.ToString());
            return "- ERROR: Please check the information you entered and try again.";
        }
    }

    //Actualización Estado Servicio
    [WebMethod(), SoapHeader("LoginCredential", Direction = SoapHeaderDirection.InOut)]
    public string SVC_ActualizaEstado(string idServicioSFDC, string estadoServicio, string Comentario)
    {
        try
        {
            if (authenticateSOAP())
            {
                LogErrorController.Insert(PropertiesApp.Error.Service, "Success");
                return ServicesController.SetStatus(idServicioSFDC, estadoServicio, Comentario);
            }

            return "- Login or Password is incorrect.";
        }
        catch
        {
            return "- ERROR: Please check the information you entered and try again.";
        }
    }


    //Creación de Caso
    [WebMethod(), SoapHeader("LoginCredential", Direction = SoapHeaderDirection.InOut)]
    public string CASO_Creacion(string idCasoSFDC, string idCaso, string tipoCaso, string motivoCaso, string problemaEncontrado, string estadoCaso, DateTime fechaHoraapertura, DateTime fechaHoraCierre, string descripcion, decimal tiempoCasoabierto, string idCuentaSFDC, string idProyectoSFDC, string idOportunidadSFDC, string idCuentaSDFC_IdClienteFnalSFDC, string idServicioSFDC)
    {
        try
        {
            if (authenticateSOAP())
            {
                LogErrorController.Insert(PropertiesApp.Error.Service, "Success");
                return CaseController.Insert(idCasoSFDC, idCaso, tipoCaso, motivoCaso, problemaEncontrado, estadoCaso, fechaHoraapertura, fechaHoraCierre, descripcion, tiempoCasoabierto, idCuentaSFDC, idProyectoSFDC, idOportunidadSFDC, idCuentaSDFC_IdClienteFnalSFDC, idServicioSFDC);
            }

            return "- Login or Password is incorrect.";
        }
        catch
        {
            return "- ERROR: Please check the information you entered and try again.";
        }
    }

    //Creación Comentario Caso
    [WebMethod(), SoapHeader("LoginCredential", Direction = SoapHeaderDirection.InOut)]
    public string CASOCOMENTARIO_Creacion(string idComentarioSFDC, string codigoComentario, string tipoComentario, string descripcion, DateTime fechaComentario, string idCasoSFDC)
    {
        try
        {
            if (authenticateSOAP())
            {
                LogErrorController.Insert(PropertiesApp.Error.Service, "Success");
                return CaseCommentController.Insert(idComentarioSFDC, codigoComentario, tipoComentario, descripcion, fechaComentario, idCasoSFDC);
            }

            return "- Login or Password is incorrect.";
        }
        catch
        {
            return "- ERROR: Please check the information you entered and try again.";
        }
    }



    #endregion

}

public class LoginInfo : SoapHeader
{
    public string login;
    public string password;
}






